#include "UEdit.h"
#include "UComboBox.h"

using namespace std;

TComboBox::TComboBox(TRectangle rect)
: TControl(rect)
{
	Edit = new TEdit(rect);
	DroppedDown = false;
	MaxTextWidth	= Right-Left-10;
	TotalStrHeight	= 0;
	RealBottom = Bottom;

	Edit->onChange = boost::bind(&TComboBox::OnEditChange, this);
}

void TComboBox::OnEditChange()
{
	string Text= Edit->GetText();
	
	for (size_t i=0; i < Items.size(); ++i)
	{
		string TempText =Items[i];
			
		if (Text.size()<=TempText.size())
		{
			TempText.erase(Text.size(), max(TempText.size()-Text.size(), (uint)0));
			if (TempText==Text)
			{	
				Edit->SetText(Items[i]);
			}
		}
	}

	if (onChange) onChange();
}

string TComboBox::GetText()
{
	return Edit->GetText();
}

void TComboBox::SetText(string Text)
{
	Edit->SetText(Text);
}


void TComboBox::AddItem(std::string Item)
{
	Items.push_back(Item);

	MaxTextWidth= max(MaxTextWidth,  Edit->Font->TextWidth(Item.c_str()));
	TotalStrHeight+= (int)Edit->Font->Height();
}

int TComboBox::MouseIndex(int X, int Y)
{
	if ((X>Left && X<Left+MaxTextWidth) && (Y>RealBottom && Y<RealBottom+TotalStrHeight))
		return (int)((Y-RealBottom)/Edit->Font->Height());

	return -1;
}

void TComboBox::Draw()
{
	Edit->Draw();
	
	glColor3f(0.8, 0.8, 0.8);
	glBegin(GL_QUADS);
		glVertex2i(Right-10	, Top);
		glVertex2i(Right	, Top);
		glVertex2i(Right	, RealBottom);
		glVertex2i(Right-10	, RealBottom);
	glEnd();

	if (DroppedDown && IsFocussed())
	{
		Bottom = RealBottom+TotalStrHeight+5;

		glColor3f(0.9, 0.9, 0.9);
 		glBegin(GL_QUADS);
			glVertex2i(Left					, RealBottom);
 			glVertex2i(Left+MaxTextWidth+10	, RealBottom);
 			glVertex2i(Left+MaxTextWidth+10	, RealBottom+TotalStrHeight+5);
 			glVertex2i(Left					, RealBottom+TotalStrHeight+5);
		glEnd();


		int mouseindex = MouseIndex(LastMX, LastMY);
		float FontHeight = Edit->Font->Height();

		if (mouseindex>-1)// mouse over the menu
		{
			glColor3f(0, 0 , 0.9);
 			glBegin(GL_QUADS);
 				glVertex2f(Left+1				, RealBottom+2+mouseindex*FontHeight);
 				glVertex2f(Left+MaxTextWidth+9	, RealBottom+2+mouseindex*FontHeight);
 				glVertex2f(Left+MaxTextWidth+9	, RealBottom+3+(mouseindex+1)*FontHeight);
 				glVertex2f(Left+1				, RealBottom+3+(mouseindex+1)*FontHeight);
			glEnd();

			glColor3f(0.8, 0.8 , 1);
 			glBegin(GL_QUADS);
 				glVertex2f(Left+2				, RealBottom+3+mouseindex*FontHeight);
 				glVertex2f(Left+MaxTextWidth+8	, RealBottom+3+mouseindex*FontHeight);
 				glVertex2f(Left+MaxTextWidth+8	, RealBottom+2+(mouseindex+1)*FontHeight);
 				glVertex2f(Left+2				, RealBottom+2+(mouseindex+1)*FontHeight);
			glEnd();
		}
		
		glColor3f(0, 0, 0);
		
		for (size_t i=0; i < Items.size(); ++i)
		{
			Edit->Font->drawText(Left+5, RealBottom+i*FontHeight, Items[i].c_str());
		}
		glColor3f(1, 1, 1);
		
	}
	else 
	{
		DroppedDown = false;
		Bottom = RealBottom;
	}
}

void TComboBox::MouseDown(int X, int Y, uint8 button)
{
	Edit->MouseDown(X, Y, button);
	if ((X>Right-10)&&(X<Right)&&(Y>Top) &&(Y<Bottom))
		DroppedDown = true;

	TControl::MouseDown(X, Y, button);

}

void TComboBox::MouseUp(int X, int Y, uint8 button)
{
	Edit->MouseUp(X, Y, button);
	TControl::MouseUp(X, Y, button);

	uint32 Index = MouseIndex(X, Y);
	if (Index>=0 && Index<=Items.size())
	{
		Edit->SetText(Items[Index]);
		DroppedDown = false;
		if (onChange) onChange();
	}
}

void TComboBox::MouseMove(int X, int Y, int mstate)
{
	Edit->MouseMove(X, Y, mstate);
	TControl::MouseMove(X, Y, mstate);
}

void TComboBox::KeyDown(SDL_KeyboardEvent* Key)
{
	Edit->KeyDown(Key);
	TControl::KeyDown(Key);
}

void TComboBox::setFocus(bool focus)
{
	TControl::setFocus(focus);
	Edit->setFocus(focus);
}


